﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace evolutionary_algorithm
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
 private ChartStyleGridlines cs; 
        private DataCollection dc; 
        private DataSeries ds;
        private Genetic genetic;

        public MainWindow() 
        { 
            InitializeComponent();
            genetic = new Genetic(40, 16);

            while (genetic.haveFound() != 1)
            {
                genetic.generate(3);
                if (genetic.doMutation)
                    genetic.mutation();

                if (genetic.haveFound() == 2)
                    genetic.changeOneGene();
            }
        } 
        private void AddChart()
        {
           
          
            cs = new ChartStyleGridlines(); 
            dc = new DataCollection(); 
             ds = new DataSeries(); 
            cs.ChartCanvas = chartCanvas; 
            cs.TextCanvas = textCanvas; 
            cs.Title = "Algorytm genetyczny (niebieski - średnia; czerwony - max)"; 
            cs.Xmin = 0;
            cs.Xmax = (double)genetic.counter-1;
            cs.Ymin = 0; 
            cs.Ymax = 1; 
            cs.YTick = 0.05;
            cs.XTick = 5;
            cs.GridlinePattern = ChartStyleGridlines.GridlinePatternEnum.Dot; 
            cs.GridlineColor = Brushes.Black; 
            cs.AddChartStyle(tbTitle,tbXLabel,tbYLabel); 
            // Draw Sine curve: 
            ds.LineColor = Brushes.Blue; 
            ds.LineThickness = 2;
            for (int i = 0; i < (int)genetic.counter; i++) 
            { 
                double x = i ; 
                double y = (double)genetic.averageFitness[i]; 
                ds.LineSeries.Points.Add(new Point(x, y)); 
            } 
            dc.DataList.Add(ds); 
            // Draw cosine curve: 
            ds = new DataSeries(); 
            ds.LineColor = Brushes.Red; 
            //ds.LinePattern = DataSeries.LinePatternEnum.DashDot; 
            ds.LineThickness = 2;
            for (int i = 0; i < (int)genetic.counter; i++) 
            { 
                double x = i;
                double y = (double)genetic.maxFitness[i];
                ds.LineSeries.Points.Add(new Point(x, y)); 
            } 
            dc.DataList.Add(ds); 
            dc.AddLines(cs); 
        } 
        private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e) 
        { 
            textCanvas.Width = chartGrid.ActualWidth; 
            textCanvas.Height = chartGrid.ActualHeight; 
            chartCanvas.Children.Clear(); 
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1); 
            AddChart();   
        } 
    } 
} 